package com.studentsystem.util;

import com.studentsystem.dataobject.CourseDO;
import com.studentsystem.model.Course;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Arrays;

@Component
public class CourseUtil {
    public Course convertDO(CourseDO courseDO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDO, course);

        if (!StringUtils.isEmpty(courseDO.getNumber())) {
            String[] strings = courseDO.getNumber().split(",");
            course.setNumber(Arrays.asList(strings));
        }

        if (!StringUtils.isEmpty(courseDO.getSuspendWeek())) {
            String[] strings = courseDO.getSuspendWeek().substring(1).split("\\|");
            course.setSuspendWeek(Arrays.asList(strings));
        }

        if (!StringUtils.isEmpty(courseDO.getClasses())) {
            String[] strings = courseDO.getClasses().substring(1).split("\\|");
            course.setClasses(Arrays.asList(strings));
        }

        return course;
    }

    public CourseDO convertModel(Course course) {
        CourseDO courseDO = new CourseDO();
        BeanUtils.copyProperties(course, courseDO);

        if (course.getNumber() != null) {
            courseDO.setNumber(String.join(",", course.getNumber()));
        } else {
            courseDO.setNumber("");
        }

        if (course.getSuspendWeek() != null) {
            courseDO.setSuspendWeek("|" + String.join("|", course.getSuspendWeek()) + "|");
        } else {
            courseDO.setSuspendWeek("");
        }

        if (course.getClasses() != null) {
            courseDO.setClasses("|" + String.join("|", course.getClasses()) + "|");
        } else {
            courseDO.setClasses("");
        }

        return courseDO;
    }
}
